package com.association.main.controller;

import com.association.main.common.R;
import com.association.main.domain.Events;
import com.association.main.domain.Members;
import com.association.main.domain.Signups;
import com.association.main.dto.SignupsDto;
import com.association.main.service.EventsService;
import com.association.main.service.MembersService;
import com.association.main.service.SignupsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/signups")
public class SignupsController {
    @Autowired
    private SignupsService signupsService;

    @Autowired
    private MembersService membersService;

    @Autowired
    private EventsService eventsService;


    @GetMapping("/event")
    public R<List<SignupsDto>> signupsList(int id){
        LambdaQueryWrapper<Signups> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Signups::getCreatedAt);
        lambdaQueryWrapper.eq(Signups::getEventId,id);
        List<Signups> list = signupsService.list(lambdaQueryWrapper);
        List<SignupsDto> signupsDtos=list.stream().map((item)->{
            SignupsDto signupsDto=new SignupsDto();
            BeanUtils.copyProperties(item,signupsDto);
            Events events = eventsService.getById(item.getEventId());
            Members members=membersService.getById(item.getMemberId());
            signupsDto.setEvents(events);
            signupsDto.setMembers(members);
            return signupsDto;
        }).collect(Collectors.toList());
        return R.success(signupsDtos);
    }
}
